import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class work03 extends JFrame {

    public static void main(String[] args) {
        new work03();
    }

    work03 (){
        add(panel());
        setBounds(200,200,300,500);
        setVisible(true);
        setAlwaysOnTop(true);
        setDefaultCloseOperation(3);
    }
    static JPanel panel (){
        JPanel Panel = new JPanel();
        JLabel name = new JLabel("用户名:");
        JTextField name_ = new JTextField(20);

        JLabel password = new JLabel("密码：");
        JTextField password_ = new JTextField(20);


        JLabel sex_ = new JLabel();
        JLabel sex = new JLabel("性别：");
        JRadioButton man = new JRadioButton("男");
        JRadioButton girl = new JRadioButton("女");
        ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(man);
        buttonGroup.add(girl);
        man.addActionListener(e ->sex_.setText(man.getText()));
        girl.addActionListener(e ->sex_.setText(girl.getText()));

        JLabel hobby = new JLabel("爱好:");

        JCheckBox sing = new JCheckBox("唱歌");
        JCheckBox dance = new JCheckBox("跳舞");
        JCheckBox swimming = new JCheckBox("游泳");
        JCheckBox trip = new JCheckBox("旅游");

        StringBuilder sb = new StringBuilder();


        sing.addItemListener(e -> sb .append(sing.getText()+"、"));

        dance.addItemListener(e -> sb.append(dance.getText()+"、"));

        swimming.addItemListener(e -> sb.append(swimming.getText()+"、"));

        trip.addItemListener(e-> sb.append(trip.getText()+"、"));



        JLabel datecon = new JLabel("出生日期");

        JLabel birthday = new JLabel();
        JComboBox<String> jComboBox = new JComboBox<>();

        Calendar instance = Calendar.getInstance();
        SimpleDateFormat simpleFormatter = new SimpleDateFormat("yyyy-MM-dd");
        for (int i = 0; i < 10; i++) {
            jComboBox.addItem(simpleFormatter.format(instance.getTime()));
            instance.add(Calendar.DATE,-1);
        }
        final String[] date = new String[1];
        jComboBox.addItemListener(e -> birthday.setText(date.toString()));

        JButton sure = new JButton("确定");

        sure.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null,
                        "用户名："+name_.getText()+"\n"+"密码："+ password_.getText()+"\n性别："
                                +sex_.getText()+"\n爱好:"+sb+"\n生日："+ date[0],
                        "提示",
                        JOptionPane.INFORMATION_MESSAGE);
            }
        });

        JButton exit = new JButton("退出");

        exit.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

        Panel.add(name, BorderLayout.NORTH);
        Panel.add(name_, BorderLayout.NORTH);
        Panel.add(password, BorderLayout.NORTH);
        Panel.add(password_, BorderLayout.NORTH);
        Panel.add(sex, BorderLayout.NORTH);
        Panel.add(man, BorderLayout.NORTH);
        Panel.add(girl, BorderLayout.NORTH);

        Panel.add(hobby, BorderLayout.CENTER);
        Panel.add(sing, BorderLayout.CENTER);
        Panel.add(dance, BorderLayout.CENTER);
        Panel.add(swimming, BorderLayout.CENTER);
        Panel.add(trip, BorderLayout.CENTER);

        Panel.add(datecon, BorderLayout.SOUTH);
        Panel.add(jComboBox, BorderLayout.SOUTH);
        Panel.add(sure, BorderLayout.SOUTH);
        Panel.add(exit, BorderLayout.SOUTH);


        return Panel;
    }
}
